﻿
#include <stddef.h>
#include "n2base.h"
#include "SectionTable.h"
#include "SymbolTable.h"

SectionTable * allsections = NULL;

UniverTabItem* SectionTable::allocGroupData(size_t count)
{
    return new Section[count];
}

Section* SectionTable::getSection(size_t index)
{
    return static_cast<Section*> (getItem(index));
}

SectionTable::SectionTable(size_t  groupCubage) : UniverTab(groupCubage)
{
}

SectionTable::SectionTable() : UniverTab(512)
{
}

void Section::moveFrom(UniverTabItem *p)
{
    Section * pSec;

    pSec = static_cast<Section*> (p);
    m_secName.setstr(pSec->m_secName.c_str());
    m_offsetInObj = pSec->m_offsetInObj;
    m_secDataSize = pSec->m_secDataSize;
    m_flags = pSec->m_flags;
    m_relInfoOffset = pSec->m_relInfoOffset;
    m_relInfoCount = pSec->m_relInfoCount;
    m_globalSectionId = pSec->m_globalSectionId;
    m_objFileIndex = pSec->m_objFileIndex;
    m_relInfoTab = pSec->m_relInfoTab;

    pSec->m_relInfoTab = NULL;
}

void Section::copyFrom(const UniverTabItem *p)
{
    const Section * pSec;

    pSec = static_cast<const Section*> (p);
    m_secName.setstr(pSec->m_secName.c_str());
    m_offsetInObj = pSec->m_offsetInObj;
    m_secDataSize = pSec->m_secDataSize;
    m_flags = pSec->m_flags;
    m_relInfoOffset = pSec->m_relInfoOffset;
    m_relInfoCount = pSec->m_relInfoCount;
    m_globalSectionId = pSec->m_globalSectionId;
    m_objFileIndex = pSec->m_objFileIndex;

    if (pSec->m_relInfoTab)
    {
        m_relInfoTab = new CoffRelInfoEntry[pSec->m_relInfoCount];
        if (m_relInfoTab)
        {
            for (n2u32 i = 0; i < pSec->m_relInfoCount; i++)
                m_relInfoTab[i] = pSec->m_relInfoTab[i];
        }
    }
    else
        m_relInfoTab = pSec->m_relInfoTab;
}

UniverTabItem * Section::getOffsetItem(size_t index)
{
    return &this[index];
}

Section::~Section()
{
    if (m_relInfoTab) {
        delete[] m_relInfoTab;
        m_relInfoTab = NULL;
    }
}

Section::Section()
{
    m_secName.setstr("");
    m_offsetInObj = 0;
    m_secDataSize = 0;
    m_flags = 0;
    m_relInfoOffset = 0;
    m_relInfoCount = 0;
    m_globalSectionId = 0;
    m_objFileIndex = 0xffffffff;
    m_relInfoTab = NULL;
}

Section::Section(const Coff32_Shdr *pCoff32Section, const char * name)
{
    m_secName.setstr(name);
    
    m_offsetInObj = pCoff32Section->raw_offset;
    m_secDataSize = pCoff32Section->raw_size;
    m_flags = pCoff32Section->flag;
    m_relInfoOffset = pCoff32Section->relinfo_offset;
    m_relInfoCount = pCoff32Section->relinfo_count;

    m_globalSectionId = 0;
    m_objFileIndex = 0xffffffff;
    m_relInfoTab = NULL;
}

unsigned int Section::getAlignValue()
{
    unsigned int index;
    static unsigned short align[] = { 1, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 1 };
    index = m_flags & 0x00F00000;
    index >>= 20;
    return align[index];
}




